package com.uhylab.uhuvinetest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

import java.text.DecimalFormat;

/**
 * Created by xyb on 15/12/16 at UHylab
 *
 * @version 1.0
 */
public class CircleBar extends View {

    private RectF mColorWheelRectangle = new RectF();
    private Paint mDefaultWheelPaint;
    private Paint mColorWheelPaint;
    private Paint mColorWheelPaintCentre;
    private Paint mTextP, mTextnum, mTextch;
    private float circleStrokeWidth;
    private float mSweepAnglePer = 1;
    //    private int mPercent;
    private int stepnumber, stepnumbernow = 60;
    private float pressExtraStrokeWidth;
    private BarAnimation anim;
    private int stepnumbermax = 60;// 默认最大步数
    private float mPercent_y, stepnumber_y, Text_y;
    private boolean isopen = false;
    private DecimalFormat fnum = new DecimalFormat("#");

    public CircleBar(Context context) {
        super(context);
        init(null, 0);
    }

    public CircleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public CircleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {

        mColorWheelPaint = new Paint();
        mColorWheelPaint.setColor(Color.rgb(109, 194, 241));
        mColorWheelPaint.setStyle(Paint.Style.STROKE);// 空心
        mColorWheelPaint.setStrokeCap(Paint.Cap.ROUND);// 圆角画笔
        mColorWheelPaint.setAntiAlias(true);// 去锯齿

        mColorWheelPaintCentre = new Paint();
        mColorWheelPaintCentre.setColor(Color.rgb(236, 236, 236));
        mColorWheelPaintCentre.setStyle(Paint.Style.STROKE);
        mColorWheelPaintCentre.setStrokeCap(Paint.Cap.ROUND);
        mColorWheelPaintCentre.setAntiAlias(true);

        mDefaultWheelPaint = new Paint();
        mDefaultWheelPaint.setColor(Color.rgb(127, 127, 127));
        mDefaultWheelPaint.setStyle(Paint.Style.STROKE);
        mDefaultWheelPaint.setStrokeCap(Paint.Cap.ROUND);
        mDefaultWheelPaint.setAntiAlias(true);


        mTextnum = new Paint();
        mTextnum.setAntiAlias(true);
        mTextnum.setColor(Color.rgb(109, 194, 241));

        mTextch = new Paint();
        mTextch.setAntiAlias(true);
        mTextch.setColor(Color.rgb(109, 194, 241));

        anim = new BarAnimation();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawArc(mColorWheelRectangle, 0, 360, false, mDefaultWheelPaint);
        canvas.drawArc(mColorWheelRectangle, 0, 360, false,
                mColorWheelPaintCentre);
        canvas.drawArc(mColorWheelRectangle, -90, mSweepAnglePer, false,
                mColorWheelPaint);
        if (isopen) {
            canvas.drawText(stepnumbernow + "", mColorWheelRectangle.centerX()
                            - (mTextnum.measureText(String.valueOf(stepnumbernow)) / 2),
                    stepnumber_y, mTextnum);
        } else {
            canvas.drawText("点击", mColorWheelRectangle.centerX()
                            - (mTextnum.measureText("点击") / 2),
                    stepnumber_y, mTextnum);
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = getDefaultSize(getSuggestedMinimumHeight(),
                heightMeasureSpec);
        int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        int min = Math.min(width, height);// 获取View最短边的长度
        setMeasuredDimension(min, min);// 强制改View为以最短边为长度的正方形
        circleStrokeWidth = Textscale(40, min);// 圆弧的宽度
        pressExtraStrokeWidth = Textscale(2, min);// 圆弧离矩形的距离
        mColorWheelRectangle.set(circleStrokeWidth + pressExtraStrokeWidth,
                circleStrokeWidth + pressExtraStrokeWidth, min
                        - circleStrokeWidth - pressExtraStrokeWidth, min
                        - circleStrokeWidth - pressExtraStrokeWidth);// 设置矩形
        mTextnum.setTextSize(Textscale(80, min));
        mTextch.setTextSize(Textscale(60, min));
        stepnumber_y = Textscale(283, min);
        Text_y = Textscale(400, min);
        mColorWheelPaint.setStrokeWidth(circleStrokeWidth);
        mColorWheelPaintCentre.setStrokeWidth(circleStrokeWidth);
        mDefaultWheelPaint
                .setStrokeWidth(circleStrokeWidth - Textscale(2, min));
        mDefaultWheelPaint.setShadowLayer(Textscale(10, min), 0, 0,
                Color.rgb(127, 127, 127));// 设置阴影

    }

    /**
     * 进度条动画
     *
     * @author Administrator
     */
    public class BarAnimation extends Animation {
        public BarAnimation() {

        }

        /**
         * 每次系统调用这个方法时， 改变mSweepAnglePer，mPercent，stepnumbernow的值，
         * 然后调用postInvalidate()不停的绘制view。
         */
        @Override
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            isopen = true;
            if (interpolatedTime < 1) {
//                mPercent = stepnumbermax - (int) Float.parseFloat(fnum.format(interpolatedTime
//                        * stepnumber * 60 / stepnumbermax));// 将浮点值四舍五入保留一位小数
                mSweepAnglePer = interpolatedTime * stepnumber * 360
                        / stepnumbermax;
                stepnumbernow = stepnumbermax - (int) (interpolatedTime * stepnumber);
            } else {
//                mPercent = stepnumbermax - (int) Float.parseFloat(fnum.format(stepnumber * 60
//                        / stepnumbermax));// 将浮点值四舍五入保留一位小数
                mSweepAnglePer = stepnumber * 360 / stepnumbermax;
                stepnumbernow = stepnumbermax - stepnumber;
            }
            postInvalidate();
        }


    }

    public void Restart() {
        mSweepAnglePer = 1;
        stepnumbernow = 60;
    }

    public void onPause() {
        isopen = false;
    }


    /**
     * 根据控件的大小改变绝对位置的比例
     *
     * @param n
     * @param m
     * @return
     */
    public float Textscale(float n, float m) {
        return n / 500 * m;
    }

    /**
     * 更新步数和设置一圈动画时间
     *
     * @param stepnumber
     * @param time
     */
    public void update(int stepnumber, int time) {
        this.stepnumber = stepnumber;
        LinearInterpolator interpolator = new LinearInterpolator();
        anim.setInterpolator(interpolator);
        anim.setDuration(time);
//		setAnimationTime(time);
        this.startAnimation(anim);
    }

    /**
     * 设置每天的最大步数
     *
     * @param Maxstepnumber
     */
    public void setMaxstepnumber(int Maxstepnumber) {
        stepnumbermax = Maxstepnumber;
    }

    /**
     * 设置进度条颜色
     *
     * @param red
     * @param green
     * @param blue
     */
    public void setColor(int red, int green, int blue) {
        mColorWheelPaint.setColor(Color.rgb(red, green, blue));
    }

    /**
     * 设置动画时间
     *
     * @param time
     */
    public void setAnimationTime(int time) {
        anim.setDuration(time * stepnumber / stepnumbermax);// 按照比例设置动画执行时间
    }


    public BarAnimation getBarAnimation() {

        return anim != null ? anim : null;
    }
}